﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace MediSIIM.BizComponent.GuiZhouSASSYB
{
    public class YiBaoJKDLL
    {
        [DllImport("YhGzHisInterface.dll", CharSet = CharSet.Ansi)]
        static extern bool yh_interface_init(
                string astr_jysr_xml,
    [MarshalAs(UnmanagedType.I8)]ref long aint_appcode,
[MarshalAs(UnmanagedType.VBByRefStr)]ref string astr_appmsg
            );
        [DllImport("YhGzHisInterface.dll", CharSet = CharSet.Ansi)]
        static extern int yh_interface_destroy();
        [DllImport("YhGzHisInterface.dll", CharSet = CharSet.Ansi)]
        static extern int yh_interface_call(string astr_jybh, string astr_jysr_xml, string astr_jylsh, [MarshalAs(UnmanagedType.VBByRefStr)]ref string astr_jyyzm, [MarshalAs(UnmanagedType.VBByRefStr)]ref string astr_jysc_xml, [MarshalAs(UnmanagedType.I4)]ref int aint_appcode, [MarshalAs(UnmanagedType.VBByRefStr)]ref string astr_appmsg);
        [DllImport("YhGzHisInterface.dll", CharSet = CharSet.Ansi)]
        public static extern int yh_interface_confirm(
    string astr_jybh,
    string astr_jylsh,
    string astr_jyyzm,
    [MarshalAs(UnmanagedType.I4)]ref int aint_appcode,
[MarshalAs(UnmanagedType.VBByRefStr)]ref  string astr_appmsg
     );
        [DllImport("YhGzHisInterface.dll", CharSet = CharSet.Ansi)]
        public static extern void yh_interface_cancel(
    string astr_jybh,
    string astr_jylsh,
    string astr_jyyzm,
    [MarshalAs(UnmanagedType.I4)]ref int aint_appcode,
[MarshalAs(UnmanagedType.VBByRefStr)]ref  string astr_appmsg
                );
        [DllImport("YhGzHisInterface.dll", CharSet = CharSet.Ansi)]
        public static extern void yh_interface_getuncertaintytrade(
                string astr_jybh,
    [MarshalAs(UnmanagedType.VBByRefStr)]ref string astr_jgxml,
     [MarshalAs(UnmanagedType.I8)]ref long aint_appcode,
[MarshalAs(UnmanagedType.VBByRefStr)]ref  string astr_appmsg
             );
        [DllImport("YhGzHisInterface.dll", CharSet = CharSet.Ansi)]
        public static extern void yh_interface_getuncertaintytrade2(
string astr_cxtj,
 [MarshalAs(UnmanagedType.VBByRefStr)]ref string astr_jgxml,
 [MarshalAs(UnmanagedType.I4)]ref int aint_appcode,
 [MarshalAs(UnmanagedType.VBByRefStr)]ref  string astr_appmsg);
        /// <summary>
        /// 获取或设置一个值，改值指示医保接口是否初始化
        /// </summary>
        public static bool inited = false;
        /// <summary>
        /// 开始一个交易
        /// </summary>
        /// <param name="tradeid">交易类型</param>
        /// <param name="yylsh">医院流水号</param>
        /// <param name="sInData">传入参数</param>
        /// <param name="ybyydm">医疗机构编号</param>
        public static int YbCall(string tradeid, string yylsh, string sInData, ref string jyyzm, string ybyydm, ref string outdata)
        {
            if (!inited)
            {
                YbInit(ybyydm);
            }
            int result = 0;
            var errormsg = string.Empty.PadRight(3000);
            yh_interface_call(tradeid, sInData, yylsh, ref jyyzm, ref outdata, ref  result, ref errormsg);
            if (result < 0)
            {
                outdata = errormsg;
            }
            outdata = outdata.TrimEnd(' ', '\0');
            return (int)result;
        }

        /// <summary>
        /// 初始化接口
        /// </summary>
        public static void YbInit(string ybyydm)
        {
            var xml = "<?xml version=\"1.0\" encoding=\"GBK\" standalone=\"yes\" ?>" +
"<data>" +
"<akb020>" + ybyydm + "</akb020>" +
"<ykb003> http://10.168.128.11:9999/assi3wbs/services/HisInterfaceWeb</ykb003>" +
"<yab003>0401</yab003>" +
"<sybz>04</sybz>" +
"</data>";
            long aint_appcode = default(long);
            string astr_appmsg = string.Empty.PadRight(1024);
            string ls_InData = string.Empty.PadRight(3000);
            var retcode = yh_interface_init(xml, ref aint_appcode, ref astr_appmsg);
            if (aint_appcode >= 0)
            {
                inited = true;
            }
            else
            {
                throw new Exception(astr_appmsg.Trim());
            }
        }
    }
}
